#!/bin/sh
#set -x
#echo " ++ $0 "

beep 3

# 判断空目录
is_empty_dir() { 
    return `ls -A $1 | wc -l`
}


#-----------------------------------------------
#// 烧写备份系统flash_root/nandboot.bin
#-----------------------------------------------
BOOT_FILE=flash_boot/nandboot.bin
if [ -f ${BOOT_FILE} ]; then
	flash_eraseall -q /dev/mtd0 >/dev/null 2>&1;  usleep 10000
	flash_eraseall -q /dev/mtd0 >/dev/null 2>&1;  usleep 10000
	flash_eraseall -q /dev/mtd0 >/dev/null 2>&1;  usleep 10000
	nandwrite -p /dev/mtd0 ${BOOT_FILE} >/dev/null 2>&1; usleep 10000
	nandwrite -p /dev/mtd0 ${BOOT_FILE} >/dev/null 2>&1; usleep 10000
	nandwrite -p /dev/mtd0 ${BOOT_FILE} >/dev/null 2>&1; usleep 10000
	echo "----------------------------------------------------------------"
	echo " write ${BOOT_FILE}     -- OK --"
	echo "----------------------------------------------------------------"
	echo " "
fi

#-----------------------------------------------
#// 2.烧写app的出厂备份包 flash_args/adas.bin
#-----------------------------------------------
FLASHE_ARGS_FILE=flash_args/adas_backup.bin
if [ -f ${FLASHE_ARGS_FILE} ]; then
	if [ -f /mnt/flash_args/sn.txt ]; then
		#// 备份sn.txt文件
		cp -f /mnt/flash_args/sn.txt  /work/
	fi
	nandmount_args  stop; usleep 10000
	flash_eraseall -q /dev/mtd1   >/dev/null 2>&1;  usleep 10000
	flash_eraseall -q /dev/mtd1   >/dev/null 2>&1;  usleep 10000
	flash_eraseall -q /dev/mtd1   >/dev/null 2>&1;  usleep 10000
	nandmount_args  restart rw; usleep 10000
	cp -f ${FLASHE_ARGS_FILE}   /mnt/flash_args/
	if [ -f /work/sn.txt ]; then
		#// 恢复sn.txt文件
		cp -f  /work/sn.txt  /mnt/flash_args/  
	fi
	sync;  usleep 10000
	nandmount_args  restart; usleep 10000
	echo "----------------------------------------------------------------"
	echo " write ${FLASHE_ARGS_FILE} TO /mnt/flash_args/  -- OK --"
	echo "----------------------------------------------------------------"
	echo " "
fi


#-----------------------------------------------
#// 烧写内核文件flash_kernel/kernel.bin
#-----------------------------------------------
KERNEL_FILE=flash_kernel/kernel.bin
if [ -f ${KERNEL_FILE} ]; then
	flash_eraseall -q /dev/mtd2 >/dev/null 2>&1;  usleep 10000
	flash_eraseall -q /dev/mtd2 >/dev/null 2>&1;  usleep 10000
	flash_eraseall -q /dev/mtd2 >/dev/null 2>&1;  usleep 10000
	nandwrite -p /dev/mtd2 ${KERNEL_FILE} >/dev/null 2>&1; usleep 10000
	nandwrite -p /dev/mtd2 ${KERNEL_FILE} >/dev/null 2>&1; usleep 10000
	nandwrite -p /dev/mtd2 ${KERNEL_FILE} >/dev/null 2>&1; usleep 10000
	echo "----------------------------------------------------------------"
	echo " write ${KERNEL_FILE}     -- OK --"
	echo "----------------------------------------------------------------"
fi



#-----------------------------------------------
#// 拷贝应用程序
#-----------------------------------------------
FLASHE_APP_DIR=flash_app
if [ -d ${FLASHE_APP_DIR} ]; then
	if is_empty_dir ${FLASHE_APP_DIR}; then
		echo " "
	else
		nandmount_app  stop; usleep 10000
		flash_eraseall -q /dev/mtd3 >/dev/null 2>&1;  usleep 10000
		flash_eraseall -q /dev/mtd3 >/dev/null 2>&1;  usleep 10000
		flash_eraseall -q /dev/mtd3 >/dev/null 2>&1;  usleep 10000
		nandmount_app  restart; usleep 10000
		cp -Rf ${FLASHE_APP_DIR}/*   /mnt/flash_app/
		chmod -Rf +x                 /mnt/flash_app/*
		sync;  usleep 10000
		nandmount_app  restart; usleep 10000
		echo "----------------------------------------------------------------"
		echo " COPY flash_app/* TO /mnt/flash_app/  -- OK --"
		echo "----------------------------------------------------------------"
		echo " "
	fi
fi

beep  5

#-----------------------------------------------
#// led 指示灯
#-----------------------------------------------
#// Green,0=open 1=clse
devmem 0x41200038 32 0
#// Red,0=open 1=clse
devmem 0x4120003c 32 1

exit 0

